<template>
	<view>
		<button type="primary" @click="getDeviceInfo">发送信息</button>
	</view>
</template>

<script>
	import * as DeviceApi from '@/api/device/index.js';
	import AppDevice from "@/service/mqtt/sim/AppDevice";
	import {
		DeviceBasicData
	} from "@/service/mqtt/DeviceConfig";

	export default {
		data() {
			return {
				username: "799893696@qq.com",
				password: "123456",
				productKey: "r57gjbjAKEY",
				deviceName: "B8:3D:FB:CC:0F:19",
			};
		},
		onLoad() {
			// this.initMqtt();
			// clearInterval(this.pingTimer);
			// this.pingTimer = setInterval(async () => {
			// 	if (!!this.appDevice) {
			// 		console.log("ping ping ping");
			// 		this.appDevice.ping();
			// 	}
			// }, 30000);
			// this.getDeviceInfo();
		},
		onUnload() {
			// clearInterval(this.pingTimer);
		},
		methods: {
			async getDeviceInfo() {
				try {
					const param = {
						deviceName: this.deviceName,
						productKey: this.productKey,
					};
					const deviceInfoRes = await DeviceApi.getDeviceInfo(param);
					console.log("deviceInfoRes", deviceInfoRes);
					const deviceId = deviceInfoRes.obj.id;
					const deviceDataRes = await DeviceApi.getPropert({
						deviceId
					});
					console.log("deviceDataRes", deviceDataRes);
					const deviceData = deviceDataRes.obj;
					const childDevices = deviceData["childDevices"] || [];
					console.log("childDevices", childDevices);
					childDevices.push({
						pk: "R91EknwemOo",
						mac: "00:A0:50:CB:FE:3D",
						sta: 0
					})
					console.log("json", JSON.stringify(childDevices));
					const setParam = {
						deviceName: this.deviceName,
						identifier: "childDevices",
						productKey: 'r57gjbjAKEY',
						value: childDevices,
						type: "string",
					};
					// const setParam = {
					// 	deviceName: this.deviceName,
					// 	identifier: "L_MODE",
					// 	productKey: 'r57gjbjAKEY',
					// 	value: 0,
					// 	type: "string",
					// };
					const setRes = await DeviceApi.getdeviceSet(setParam);
					console.log("setRes", setRes);

				} catch (err) {
					console.log("getDeviceInfo error", err);
				}
			},
			initMqtt() {
				this.appDevice = new AppDevice();
				let deviceBasicData = new DeviceBasicData();
				deviceBasicData.username = this.username;
				deviceBasicData.password = this.password;
				deviceBasicData.clientId = this.appDevice.getClientid(deviceBasicData.username, deviceBasicData.password);
				deviceBasicData.addr = "47.97.72.174:1887" //"192.168.1.66:1887"//"47.97.72.174:1887";

				let that = this;
				this.appDevice.serverDataListener = {
					onPushProperty: function(payload) {
						console.log("收到属性推送:", payload.params);
					},
					onPushEvent: function(payload) {
						console.log("收到事件推送:", payload.params);
					},
					onResSetProperty: function(payload) {
						console.log("收到属性设置回复:", payload);
					},
					onReService: function(payload) {
						console.log("服务调用回复:", payload);
					},
					onConnect(sucess) {
						console.log("连接服务器:", sucess);
						// showInfo("连接mqtt服务器" + (sucess ? '成功' : '失败'));
						that.appDevice.subscribe(that.productKey, that.deviceName);
					},
					onSubAck(sucess) {
						console.log("监听topic反馈:", sucess);
					},
					onPushAck(sucess) {
						console.log("push反馈:", sucess);
					},
					onClose() {
						console.log("连接关闭");
					},
				};
				this.appDevice.init(deviceBasicData);
				this.appDevice.connectServer();
			},
			sendData() {
				const keyValuePair = {
					productKey: {
						type: "string",
						value: "r57gjbjAKEY"
					},
					deviceName: {
						type: "string",
						value: "B8:3D:FB:CC:0F:19"
					}
				};
				const param = {
					deviceName: "B8:3D:FB:CC:0F:19",
					productKey: "r57gjbjAKEY",
					identifier: "search",
					map: keyValuePair,
				}
				console.log("param", param);
				DeviceApi.invokeService(param).then(res => {
					console.log("invokeService success", res);
				}).catch(err => {
					console.log("invokeService fail", error);
				})
			},
		}
	}
</script>

<style lang="scss">

</style>
